package com.link510.nbiot.cmcc.invokeapi;

import com.alibaba.fastjson.JSON;
import com.link510.nbiot.cmcc.NBIoTClient;
import com.link510.nbiot.cmcc.domain.device.*;
import com.link510.nbiot.cmcc.domain.OnenetMessageInfo;
import com.link510.nbiot.cmcc.exption.NBStatus;
import com.link510.nbiot.cmcc.exption.OnenetNBException;

/**
 * 设备管理
 */
public class DeviceManagement extends BaseManagement {

    private NBIoTClient nbIoTClient;

    private DeviceManagement() {

    }

    public DeviceManagement(NBIoTClient nbIoTClient) {
        this.nbIoTClient = nbIoTClient;
    }


    /**
     * 设备注册
     *
     * @param deviceDTO 设备注册实体
     * @return 返回注册消息
     * @throws OnenetNBException OnenetNBException
     */
    public RegistryDeviceOutDTO registry(RegistryDeviceDTO deviceDTO) throws OnenetNBException {

        try {

            OnenetMessageInfo messageInfo = this.nbIoTClient.restJsonPost(deviceDTO.toUrl(), deviceDTO.toMaps(), OnenetMessageInfo.class);


            validateMessageInfo(messageInfo);

            RegistryDeviceOutDTO registryDeviceOutDTO = JSON.parseObject(messageInfo.getData(), RegistryDeviceOutDTO.class);

            if (registryDeviceOutDTO == null) {
                throw new OnenetNBException(NBStatus.PARSER_DATA_ERROR, "数据解析异常2");
            }

            return registryDeviceOutDTO;
        } catch (OnenetNBException ex) {
            throw ex;
        } catch (Exception ex) {

            throw new OnenetNBException(NBStatus.PARSER_DATA_ERROR, "数据解析异常");
        }
    }

    /**
     * 更新设备信息
     *
     * @param deviceDTO 设备信息
     * @return 返回更新内容
     * @throws OnenetNBException OnenetNBException
     */
    public boolean update(UpdateDeviceDTO deviceDTO) throws OnenetNBException {

        try {

            OnenetMessageInfo messageInfo = this.nbIoTClient.restJsonPost(deviceDTO.toUrl(), deviceDTO.toMaps(), OnenetMessageInfo.class);

            validateMessageInfo(messageInfo);

            return true;
        } catch (OnenetNBException ex) {
            throw ex;
        } catch (Exception ex) {

            throw new OnenetNBException(NBStatus.PARSER_DATA_ERROR, "数据解析异常");
        }
    }

    /**
     * 查找设备
     *
     * @param deviceId 设备Id
     * @return 返回消息
     * @throws OnenetNBException 异常
     */
    public FindDeviceOutDTO find(String deviceId) throws OnenetNBException {


        try {

            OnenetMessageInfo messageInfo = this.nbIoTClient.restGet(new FindDeviceDTO(deviceId).toUrl(), OnenetMessageInfo.class);

            validateMessageInfo(messageInfo);

            FindDeviceOutDTO findDeviceOutDTO = JSON.parseObject(messageInfo.getData(), FindDeviceOutDTO.class);

            if (findDeviceOutDTO == null) {
                throw new OnenetNBException(NBStatus.PARSER_DATA_ERROR, "数据解析异常2");
            }
            return findDeviceOutDTO;

        } catch (OnenetNBException ex) {
            throw ex;
        } catch (Exception ex) {

            throw new OnenetNBException(NBStatus.PARSER_DATA_ERROR, "数据解析异常");
        }
    }

    /**
     * 根据imei查询设备信息
     *
     * @param imei imei
     * @return
     * @throws OnenetNBException
     */
    public FindIMEIDeviceOutDTO findImei(String imei) throws OnenetNBException {

        try {
            OnenetMessageInfo messageInfo = this.nbIoTClient.restGet(new FindIMEIDeviceDTO(imei).toUrl(), OnenetMessageInfo.class);

            validateMessageInfo(messageInfo);

            FindIMEIDeviceOutDTO findIMEIDeviceOutDTO = JSON.parseObject(messageInfo.getData(), FindIMEIDeviceOutDTO.class);

            if (findIMEIDeviceOutDTO == null) {
                throw new OnenetNBException(NBStatus.PARSER_DATA_ERROR, "数据解析异常2");
            }
            return findIMEIDeviceOutDTO;
        } catch (OnenetNBException ex) {
            throw ex;
        } catch (Exception ex) {

            throw new OnenetNBException(NBStatus.PARSER_DATA_ERROR, "数据解析异常");
        }

    }


    /**
     * 删除设备
     *
     * @param deviceId 设备Id
     * @return 返回消息
     * @throws OnenetNBException 异常
     */
    public boolean delete(String deviceId) throws OnenetNBException {

        try {

            OnenetMessageInfo messageInfo = this.nbIoTClient.restDelete(new DeleteDeviceDTO(deviceId).toUrl(), OnenetMessageInfo.class);

            validateMessageInfo(messageInfo);

            return true;


        } catch (OnenetNBException ex) {
            throw ex;
        } catch (Exception ex) {

            throw new OnenetNBException(NBStatus.PARSER_DATA_ERROR, "数据解析异常");
        }
    }
}
